package ru.ganyaev.external_process;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Command {
    private String program;
    private List<String> arguments = new ArrayList<String>();

    public Command(String program, String argument) {
        this.program = program;
        this.arguments.add(argument);
    }

    public Command(String program, List<String> arguments) {
        this.program = program;
        this.arguments.addAll(arguments);
    }

    public Command(String program, String... arguments) {
        this.program = program;
        this.arguments.addAll(Arrays.asList(arguments));
    }

    public String getProgram() {
        return program;
    }

    public List<String> getArguments() {
        return arguments;
    }

    public List<String> getCommandAsList() {
        List<String> command = new ArrayList<String>();

        command.add(this.program);
        command.addAll(this.arguments);

        return command;
    }

    @Override
    public String toString() {
        String command = "Command [" + frame(program);

        for (String arg : arguments) {
            command = command + " " + frame(arg);
        }

        return command + "]";
    }

    private String frame(String string) {
        if (string.contains(" ")) {
            return "\"" + string + "\"";
        } else {
            return string;
        }
    }
}
